package com.resifind.www.service.impl;

import com.resifind.www.constant.*;
import com.resifind.www.dao.*;
import com.resifind.www.dao.impl.*;
import com.resifind.www.exception.BaseException;
import com.resifind.www.exception.Resp;
import com.resifind.www.po.*;
import com.resifind.www.service.MerchantService;
import com.resifind.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 36069
 */
public class MerchantServiceImpl implements MerchantService {
    private final MerchantDao merchantDao = new MerchantDaoImpl();

    @Override
    public Resp<PageBean<Merchant>> queryByPageAndCondition(int currentPage, int pageSize, String tradeName, int clientId) {
        PageBean<Merchant> pageBean = new PageBean<>();
        List<Merchant> merchantList;
        int totalCount;
        if (clientId != 0 && clientId != -1) {
            // 获取符合条件的商家信息
            merchantList = merchantDao.queryOpenMerchantForClient(currentPage, pageSize, tradeName, clientId);
            // 获取总记录数
            totalCount = merchantDao.queryMerchantCountForClient(tradeName, clientId);
        } else {
            // 游客查询
            merchantList = merchantDao.queryOpenMerchantForGuest(currentPage, pageSize, tradeName);
            totalCount = merchantDao.queryMerchantCountForGuest(tradeName);
        }
        // 封装PageBean对象
        pageBean.setTotalCount(totalCount);
        pageBean.setRows(merchantList);
        return Resp.success(pageBean);
    }

    @Override
    public Resp<PageBean<Merchant>> queryByPageAndConditionForAdmin(int currentPage, int pageSize, String tradeName) {
        PageBean<Merchant> pageBean = new PageBean<>();
        // 获取符合条件的商家信息
        List<Merchant> merchantList = merchantDao.queryAllMerchantForAdmin(currentPage, pageSize, tradeName);
        // 获取总记录数
        int totalCount = merchantDao.queryMerchantCountForAdmin(tradeName);
        // 封装PageBean对象
        pageBean.setTotalCount(totalCount);
        pageBean.setRows(merchantList);
        return Resp.success(pageBean);
    }

    @Override
    public Resp<String> updateMerchant(Merchant merchant) {
        Connection conn = null;
        try {
            conn = JdbcUtils.getConn();
            JdbcUtils.beginTransaction(conn);
            boolean updateMerchantResult = merchantDao.update(merchant, conn);
            if (updateMerchantResult) {
                JdbcUtils.commitTransaction(conn);
                return Resp.success("success");
            } else {
                JdbcUtils.rollbackTransaction(conn);
                throw new BaseException(ErrorCode.DB_ERROR);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            JdbcUtils.rollbackTransaction(conn);
            throw new BaseException(ErrorCode.SERVER_ERROR);
        } finally {
            JdbcUtils.closeConn(conn, null, null);
        }
    }

    @Override
    public Resp<String> updateMerchantStatus(int merchantId, String newStatus) {
        Connection conn = null;
        try {
            conn = JdbcUtils.getConn();
            JdbcUtils.beginTransaction(conn);
            boolean updateStatusResult = merchantDao.updateMerchantStatus(merchantId, newStatus, conn);
            if (updateStatusResult) {
                JdbcUtils.commitTransaction(conn);
                return Resp.success("success");
            } else {
                JdbcUtils.rollbackTransaction(conn);
                throw new BaseException(ErrorCode.DB_ERROR);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            JdbcUtils.rollbackTransaction(conn);
            throw new BaseException(ErrorCode.SERVER_ERROR);
        } finally {
            JdbcUtils.closeConn(conn, null, null);
        }
    }

}
